﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_SuperviseUpdate : System.Web.UI.Page
{
    public string AccountId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!IsPostBack)
            {
                string strSql;
                System.Text.StringBuilder strHtml = new System.Text.StringBuilder();

                AccountId =SysFun.IsSQL(Request.QueryString["AccountId"]);
                strSql = "select supervise.*,area.areaname from supervise left join area on supervise.AreaId=Area.AreaId where AccountId=" + AccountId;
                Db db = new Db();
                System.Data.IDataReader dr = db.DataReader(strSql);
                while (dr.Read())
                {
                    txtAccountId.Text = dr["AccountId"].ToString();
                    txtAccountId.Visible = false;

                    strHtml.Append("<input type='text' value='" + dr["AreaName"].ToString() + "' name='AreaName' NotNull=\"所属地区\" />");
                    strHtml.Append("<input type='text' name='AreaId' value=" + dr["AreaId"].ToString() + " style='display: none'/>");
                    strHtml.Append("<a href='javascript:AreaSelect(530000)'>选择地区</a>");
                    LabArea.Text = strHtml.ToString();
                    strHtml.Remove(0, strHtml.Length);
                    switch (SysFun.ToTrim(dr["Class"]))
                    {
                        case "1":
                            {
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton1'name='RdoClass' value='1' checked >省级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='2'>市级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='3'>县级");
                                break;
                            }
                        case "2":
                            {
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton1'name='RdoClass' value='1' >省级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='2' checked >市级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='3'>县级");
                                break;
                            }
                        default:
                            {
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton1'name='RdoClass' value='1' >省级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='2'>市级");
                                strHtml.Append("<input type='radio' runat='server' id='RadioButton2' name='RdoClass' value='3' checked >县级");
                                break;
                            }

                    }
                    LabClass.Text = strHtml.ToString();
                    txtAddress.Text = dr["Address"].ToString();
                    txtUserCode.Text = SysFun.ToTrim(dr["UserCode"]);
                    txtEmail.Text = dr["Email"].ToString();
                    txtFax.Text = dr["Fax"].ToString();
                    txtNetAddress.Text = dr["NetAddress"].ToString();
                    txtPostCode.Text = dr["PostCode"].ToString();
                    txtSuperviseMan.Text = dr["SuperviseMan"].ToString();
                    txtSuperviseName.Text = dr["SuperviseName"].ToString();
                    txtTelephone.Text = dr["Telephone"].ToString();
                    txtRegistDate.Text = dr["RegistDate"].ToString();
                    txtRegistDate.Visible = false;
                }
                dr.Close();
                dr.Dispose();
                db.Close();
            }
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        string strclass = SysFun.IsSQL(Request.Form["RdoClass"].ToString()), strAreaId = SysFun.IsSQL(Request.Form["AreaId"].ToString());
        AccountId = SysFun.ToTrim(txtAccountId.Text);
        int rowCun = 0;
        Db db1 = new Db();
        System.Data.IDataReader dr;
        String strSql = "select count(accountid) as cun from dbo.v_account where ( dwname= '" + SysFun.ToTrim(txtSuperviseName.Text) + "' or usercode='" + SysFun.ToTrim(txtUserCode.Text) + "') and AccountId <>" + AccountId.ToString();
        dr = db1.DataReader(strSql);
        if (dr.Read())
        {
            rowCun = SysFun.ToInt(dr["cun"]);
        }
        dr.Close();
        dr.Dispose();
        db1.Close();
        if (rowCun > 0)
        {
            Response.Write("<script language=JavaScript>alert('该用户在数据库中已经存在！！！');</script>");
        }
        else
        {


            HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
            Grove.ORM.ObjectOperator oo = db.GetObjectOperator();

            HZ.MBSM.Model.Supervise Supervise = (HZ.MBSM.Model.Supervise)oo.Retrieve(typeof(HZ.MBSM.Model.Supervise), AccountId);
            //Supervise.AccountId = SysFun.ToInt(txtAccountId.Text);
            Supervise.UserCode = SysFun.ToTrim(txtUserCode.Text);
            Supervise.Address = SysFun.ToTrim(txtAddress.Text);
            Supervise.AreaId = SysFun.ToInt(strAreaId);
            Supervise.Class = strclass;
            Supervise.Email = SysFun.ToTrim(txtEmail.Text);
            Supervise.Fax = SysFun.ToTrim(txtFax.Text);
            Supervise.NetAddress = SysFun.ToTrim(txtNetAddress.Text);
            Supervise.PostCode = SysFun.ToTrim(txtPostCode.Text);
            Supervise.RegistDate = Convert.ToDateTime(txtRegistDate.Text.ToString());
            Supervise.SuperviseMan = SysFun.ToTrim(txtSuperviseMan.Text);
            Supervise.SuperviseName = SysFun.ToTrim(txtSuperviseName.Text);
            Supervise.Telephone = SysFun.ToTrim(txtTelephone.Text);
            oo.Update(Supervise);

            oo.Dispose();
            db.Close();
            Response.Write("<script>alert('保存成功！！！');</script>");

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Supervise", Supervise.UserCode + "|" + Supervise.Address + "|" + Supervise.AreaId + "|" + Supervise.Class + "|" + Supervise.RegistDate, "../Magage/SuperviseUpdate.aspx");


            Response.Write("<script>window.opener.location.reload();window.close();</script>");
        }
    }

}
